<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;


/**
 * OtcTrading模型
 */
class OtcTrading extends ModelBase
{

    protected $insert = ['create_time','serial_number'];
//    protected $field = true;
    /**
     * 流水号自动完成
     */
    public function setSerialNumberAttr()
    {

        return  'TR'.(time() + 800000000).(substr(str_shuffle('0123456789ABCDEFGHIJKLNMOPQRSTUVWXYZ'),0,4));
    }

    /**
     * 连用户表
     */
    public function member(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 连支付方式表
     */
    public function wallet(){
        return $this->belongsTo('Wallet','wallet_id','id');
    }

    /**
     * 店铺表
     */
    public function shop(){
        return $this->belongsTo('OtcShop','shop_id','id');
    }

    /**
     * 订单表
     */
    public function otcorder(){
        return $this->hasMany('OtcOrder','otc_id','id');
    }



    /**
     * 获取状态信息
     */
    public function getStateTextAttr(){
        $arr=['排队中','未完成','已完成','已取消'];
        return $arr[$this->state];
    }

    /**
     * 获取匹配次数信息
     */
    public function getPostitionAttr(){
        if( $this->state!=0){
            return 0;
        }
        $list=self::where(['state'=>0,'wallet_id'=>$this->wallet_id,'surplus_total'=>$this->surplus_total,'type'=>$this->data['type']])->order('feed_count desc ,match_count asc ,price asc ,id asc')->column('id');
        $postition=0;
        foreach ($list as $k=>$v){
            if($v==$this->id){
                $postition=$k+1;
            }
        }
        return $postition;
    }

    /**
     * 获是否被投诉
     */
    public function getIsComplaintAttr(){
        return OtcFeedback::where(['otc_id'=>$this->id])->count();
    }

    /**
     * 获取状态信息内容
     */
    public function getStateHtmlAttr(){

        switch ($this->state){
            case 1:
                $html='<span class="el-tag el-tag--mini">未完成</span>';
                break;
            case 2:
                $html='<span class="el-tag el-tag--success">已完成</span>';
                break;
            case 3:
                $html='<span class="el-tag el-tag--danger">已取消</span>';
                break;
            case 0:
                $html='<span class="el-tag el-tag--mini">排队中</span>';
                break;
        }
        return $html;
    }

    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }

    /**
     * 时间自动完成
     */
    public function getUpdateTimeAttr($value)
    {
        if($value){
            return date('Y-m-d H:i:s',$value);
        }else{
            return '';
        }

    }
    /***
     *支付方式
     */
    public function getPayTextAttr($value)
    {
        $str='';
        if($this->is_alipay){
            $str.='支付宝';
        }
        if($this->is_weixin){
            $str.=',微信';
        }
        if($this->is_bank){
            $str.=',银行卡';
        }
        return $str;
    }

    /**
     * 时间自动完成
     */
    public function getMaxMoneyAttr()
    {
        $max=floor($this->price*$this->surplus_number);

        return $max;


    }



    /**
     * 时间自动完成
     */
    public function getMinMoneyAttr($value)
    {
        $max=floor($this->price*$this->surplus_number);
        $min=$value;
        if($max<$min){
           return $max;
        }else{
            return $value;
        }

    }
}
